import user from '../api/user'
import products from '../api/products'
import orders from '../api/orders'
import carts from '../api/carts'
import { browserHistory } from 'react-router'

// user action *****************************

export const LOGIN_POST = 'LOGIN_POST'
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS'
export const LOGOUT = 'LOGOUT'
export const REGISTER_SUCCESS = 'REGISTER_SUCCESS'
export const REGISTER_USER = 'REGISTER_USER'
export const GET_MY_ORDERS = 'GET_MY_ORDERS'
export const SET_USER_INFO = 'SET_USER_INFO'

const regist = user => ({
  type: REGISTER_SUCCESS,
  user
})

const login = user => ({
  type: LOGIN_SUCCESS,
  user
})

const logout = () => ({
  type: LOGOUT
})

const setUserInfo = (userInfo) => ({
  type: SET_USER_INFO,
  userInfo
})

export const logoutUser = () => (dispatch, getState) => {
  user.logout(() => {
    window.localStorage.setItem('user', JSON.stringify({isAdmin:false,isLogin:false}))
    browserHistory.push('/')
    dispatch(logout())
  })
}

export const loginUser = (name, passwd) => (dispatch, getState) => {
  user.login(name, passwd, (user) => {
    console.log('login success')
    user['isLogin'] = true
    window.localStorage.setItem('user', JSON.stringify(user))
    dispatch(login(user))
    dispatch(getUserInfo())
    browserHistory.push('/')
  }, () => {
    alert('用户名或密码错误')
    console.log('login faild')
  })
}

export const registUser = (name, mail, passwd) => (dispatch, getState) => {
  user.regist(name, passwd, mail, () => {
    dispatch(regist({'name':name}))
    browserHistory.push('/')
    }, () => {
      console.log('fcb')
    })
}

export const getUserInfo = (userId) => (dispatch, getState) => {
  user.getInfo(userId, (userInfo) => {
    console.log('get user info success', userInfo)
    dispatch(setUserInfo(userInfo))
  }, () => {
    console.log('get user info faild')
  })
}

export const updateUserInfo = (userInfo) => (dispatch, getState) => {
  user.update(userInfo, (userInfo) => {
    console.log('update userinfo success')
    dispatch(getUserInfo(1))
  }, () => {
    console.log('update userinfo faild')
  })
}

// product action ******************************

export const SET_PRODUCTS = 'SET_PRODUCTS'
export const ADD_PRODUCT = 'ADD_PRODUCT'
export const CLEAN_FORM = 'CLEAN_FORM'

const setProducts = products => ({
  type: SET_PRODUCTS,
  products
})

const addProduct = product => ({
  type: ADD_PRODUCT,
  product
})

export const getProducts = (page) => (dispatch, getState) => {
  products.getProducts(page, (products) => {
    console.log('get product success', products)
    dispatch(setProducts(products))
  }, () => {
    console.log('get product faild')
  })
}

// export const addProducts = (product) => (dispatch, getState) => {
//   products.addProducts(products, () => {
//     console.log('add products success')
//   }, () => {
//     console.log('add products faild')
//   })
// }

export const createProduct = (product) => (dispatch, getState) => {
  products.createProduct(product, () => {
    console.log('create product success')
    dispatch(getProducts(1))
  }, () => {
    console.log('create product faild')
  })
}

// order action ****************************

export const SET_ORDERS = 'SET_ORDERS'

const setOrders = orders => ({
  type: SET_ORDERS,
  orders
})

export const getMyOrders = (page) => (dispatch, getState) => {
  orders.getAll(page, (orders) => {
    console.log('get order success')
    dispatch(setOrders(orders))
  }, () => {
    console.log('get orders faild')
  })
}

// carts actions

export const CLEAN_CART = 'CLEAN_CART'
export const ADD_CART = 'ADD_CART'
export const SET_CART = 'SET_CART'

export const setCarts = (products) => ({
  type: SET_CART,
  products
})

export const cleanCart = () => ({
  type: CLEAN_CART
})

export const addCart = product => ({
  type: ADD_CART,
  product
})

export const addToCart = product => (dispatch, getState) => {
  carts.addCart(product.id, () => {
    console.log('add product success')
    dispatch(addCart(product))
  }, () => {
    console.log('faild add product')
  })
}

export const getCarts = (userid) => (dispatch, getState) => {
  carts.getAllCart(userid, (carts) => {
    console.log('get all carts success')
    dispatch(setCarts(carts))
  }, () => {
    console.log('get all carts faild')
  })
}

export const checkout = (price) => (dispatch, getState) => {
  carts.checkout(price, () => {
    console.log('checkout successful')
    dispatch(cleanCart())
    dispatch(getMyOrders(1))
  }, () => {
    console.log('checkout faild')
  })
}